package com.taiyuan.ty.controller;

import com.taiyuan.ty.common.R;
import com.taiyuan.ty.entity.Comment;
import com.taiyuan.ty.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author : 周桓宇
 * @description :
 * @createDate : 2025/3/26 下午4:43
 */
@RestController
@RequestMapping("/api/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    /**
     * 评论
     * @param comment
     * @return
     */
    @PostMapping("/save")
    public R<String> save(@RequestBody Comment comment) {
        if (comment.getCreateTime() == null) {
            comment.setCreateTime(new Date());
        }
        if (commentService.save(comment)) {
            return R.success("评论成功");
        } else {
            return R.error("评论失败");
        }
    }

    /**
     * 删除评论
     */
    @DeleteMapping
    public R<String> delete(Long id) {
        if (commentService.removeById(id)) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

}

